[#import "/framework/JGP.htm"  as JGP]
<!--
项目   jgp-cloud-parent
作者   LOUFEI [Gavin]
时间   2022/2/17
-->
[@JGP.head "Title"]
    <style>
        .is-important{
            color: red;
        }
    </style>
[/@JGP.head]
[@JGP.body]
    <jgp-tab-panel>
        <jgp-tab _title="机构数据授权">
            <jgp-div _col="['*','*']" _cunit="px" _aux="true">
                <jgp-div slot="1-1" _row="['78','*']" _runit="px">
                    <jgp-query slot="1-1" _target="userGrid" _action="/security/user-api-ctl/query-system-user">
                        <jgp-form-group _title="查询被授权用户">
                            <jgp-text _label="用户名" _name="username"></jgp-text>
                        </jgp-form-group>
                    </jgp-query>
                    <jgp-grid slot="2-1" _id="userGrid" _check="false" _tools_name="true" _ondblclick="initSelected">
                    </jgp-grid>
                </jgp-div>
                <jgp-div slot="1-2" _row="['38','*']" _runit="px">
                    <div slot="1-1">
                        <jgp-btn _fn="savePower">保存</jgp-btn>
                    </div>
                    <jgp-panel slot="2-1" _padding="10px" _border="right">
                        <jgp-tree2 _id="tree" _root="机构" _icon="false" _extend="true"
                                   _onload="initTree" _show_root="false"
                                   _select_next_level="false"
                                   _lazy="false"
                                   _url="/security/institution-api-ctl/query-tree"></jgp-tree2>
                    </jgp-panel>
                </jgp-div>
            </jgp-div>
        </jgp-tab>
    </jgp-tab-panel>
[/@JGP.body]

[@JGP.foot]

    <script>
        var currentUserId;

        function initSelected(args) {
            currentUserId = args.back.record.id;
            JGP.tree2('tree').clearChecked();
            JGP.$get('/security/institution-user-api-ctl/query-institution-ids', {
                    userId: currentUserId
                },
                function (result) {
                    if (result.flag) {
                        JGP.$get('/security/data-power-api-ctl/query-with-institution', {
                            userId:currentUserId
                        },function(result2){
                            if(result2.flag){
                                var authorizations = result2.data.authorizations;
                                var institutions = result.data.institutions;
                                var union = authorizations.concat(institutions.filter(v=>!authorizations.includes(v)));
                                JGP.tree2('tree').setSelectedKeys(union);
                            }
                        })
                    }
                })
        }

        function savePower() {
            if (currentUserId) {
                var selected = JGP.tree2('tree').getSelected();
                var selectedKeys = [];
                for (var i = 0; i < selected.length; i++) {
                    selectedKeys.push(selected[i].id + '');
                }
                JGP.$post('/security/data-power-api-ctl/grant-with-institution', {
                    institutionIds: selectedKeys + '',
                    userId: currentUserId
                }, function (result) {

                })
            }
        }
    </script>
[/@JGP.foot]